package com.example.yeujian5.yuejian5.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.yeujian5.yuejian5.R;
import com.example.yeujian5.yuejian5.fragment.ClubFragment;
import com.example.yeujian5.yuejian5.fragment.HomeFragment;
import com.example.yeujian5.yuejian5.fragment.PersonFragment;
import com.example.yeujian5.yuejian5.fragment.PictureFragment;
import com.example.yeujian5.yuejian5.fragment.TrainFragment;

public class ContentActivity extends FragmentActivity implements View.OnClickListener {

    private LinearLayout mTabTrain;
    private LinearLayout mTabClub;
    private LinearLayout mTabHome;
    private LinearLayout mTabPicture;
    private LinearLayout mTabMe;
    private ImageButton mImgTrain;
    private ImageButton mImgClub;
    private ImageButton mImgHome;
    private ImageButton mImgPicture;
    private ImageButton mImgMe;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_content);


        initView();//初始化控件
        initEvent();//初始化事件
        setSelect(0);
    }

    private void initEvent()
    {
        mTabTrain.setOnClickListener(this);
        mTabClub.setOnClickListener(this);
        mTabHome.setOnClickListener(this);
        mTabPicture.setOnClickListener(this);
        mTabMe.setOnClickListener(this);
    }
    private void initView()

    {
        mTabTrain = (LinearLayout) findViewById(R.id.id_tab_train);
        mTabClub = (LinearLayout) findViewById(R.id.id_tab_club);
        mTabHome = (LinearLayout) findViewById(R.id.id_tab_home);
        mTabPicture = (LinearLayout) findViewById(R.id.id_tab_picture);
        mTabMe = (LinearLayout) findViewById(R.id.id_tab_me);

        mImgTrain = (ImageButton) findViewById(R.id.id_tab_train_img);
        mImgClub = (ImageButton) findViewById(R.id.id_tab_club_img);
        mImgHome = (ImageButton) findViewById(R.id.id_tab_home_img);
        mImgPicture = (ImageButton) findViewById(R.id.id_tab_picture_img);
        mImgMe = (ImageButton) findViewById(R.id.id_tab_me_img);
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new HomeFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                mImgHome.setImageResource(R.drawable.ic_home_press);
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new ClubFragment();transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);

                }
                mImgClub.setImageResource(R.drawable.ic_club_press);
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new TrainFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);
                }
                mImgTrain.setImageResource(R.drawable.ic_train_press);
                break;
            case 3:
                if (mTab04 == null)
                {
                    mTab04 = new PictureFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    transaction.show(mTab04);
                }
                mImgPicture.setImageResource(R.drawable.ic_picture_press);
                break;
            case 4:
                if (mTab05 == null)
                {
                    mTab05 = new PersonFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else
                {
                    transaction.show(mTab05);
                }
                mImgMe.setImageResource(R.drawable.ic_me_press);
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_tab_home:
                setSelect(0);
                break;
            case R.id.id_tab_club:
                setSelect(1);
                break;
            case R.id.id_tab_train:
                setSelect(2);
                break;
            case R.id.id_tab_picture:
                setSelect(3);
                break;
            case R.id.id_tab_me:
                setSelect(4);
                break;

            default:
                break;
        }
    }


    private void resetImgs()
    {
        mImgTrain.setImageResource(R.drawable.ic_train);
        mImgClub.setImageResource(R.drawable.ic_club);
        mImgHome.setImageResource(R.drawable.ic_home);
        mImgPicture.setImageResource(R.drawable.ic_picture);
        mImgMe.setImageResource(R.drawable.ic_me);
    }
}
